package CalendarProgram;

import dme.forecastiolib.FIODaily;
import dme.forecastiolib.ForecastIO;

/**
 * Retrieves the weather information from Forecast. This class creates an object that retrieves and stores the weather information.
 *
 */
public class Forecast 
{
	private final String apikey = "29bb2e47b2eded92fdcf65832bc114bb";
	private String time, latitude, longitude, address, iconText;
	private double temperatureMax;
		
	/**Retrieves and stores the weather information. 
	 * 
	 * @param time The time and date, in string form. Refer to the API for how to format the information.
	 * @param address The address in string form. 
	 */
	public Forecast(String time, String address)
	{
		this.time = time;
		this.address = address;
		//Leuven:	50.8775, 4.704444
		
		//First, get coordinates
		GeocodeImplementation g = new GeocodeImplementation();
		String jsonString = g.getJSONByGoogle(address);
		//address format = "address,city,state,zipcode" e.g. "1Diestsestraat,Leuven,3000"		
		//get necessary data out of json
		if(jsonString.contains("\"status\" : \"OK\""))
		{
			int findGeometry = jsonString.indexOf("geometry");
			int findLocation = jsonString.indexOf("location", findGeometry);
			
			//Get latitude
			int startPos = jsonString.indexOf("lat", findLocation);
			int endPos = jsonString.indexOf(",", startPos);
			String latitudeString = jsonString.substring(startPos+7, endPos);
			
			//Get longitude
			startPos = jsonString.indexOf("lng", findLocation);
			endPos = jsonString.indexOf("\n", startPos);
			String longitudeString = jsonString.substring(startPos+7, endPos);
			
			latitude = latitudeString;
			longitude = longitudeString;
		}
		else
		{
			System.out.println("There was an error with getting the google geocode coordinates");
		}
		
		//Get the actual forecast
		ForecastIO fio = new ForecastIO(apikey);
		fio.setUnits(ForecastIO.UNITS_SI);
		//time format = [YYYY]-[MM]-[DD]T[HH]:[MM]:[SS] e.g. 2013-05-06T12:00:00-0400
		fio.setTime(time);
		
	fio.getForecast(latitude,longitude);
	//^returns true/false if successful
		FIODaily daily = new FIODaily(fio);
		temperatureMax = daily.getDay(0).temperatureMax(); //gets a double value for the maximum temperature
		iconText = daily.getDay(0).icon(); //gets a description for an icon
		iconText = iconText.substring(1, iconText.length()-1); //Remove quotation marks
	}

	/** Returns the time that the weather was forecast at.
	 * @return the time
	 */
	public String getTime() {
		return time;
	}

	/** Returns the latitude of the address given.
	 * @return the latitude
	 */
	public String getLatitude() {
		return latitude;
	}

	/** Returns the longitude of the address given.
	 * @return the longitude
	 */
	public String getLongitude() {
		return longitude;
	}

	/** Returns the address at where the weather should be forecast.
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/** Returns the maximum temperate on the day of the weather forecast.
	 * @return the temperatureMaxTime
	 */
	public double getTemperatureMax() {
		return temperatureMax;
	}

	/** Returns a string with a brief description of the weather that day. Used for displaying icons.
	 * @return the iconText
	 */
	public String getIconText() {
		return iconText;
	}
}
